/// <reference path="../renderer.ts" />

namespace FIREFLYX {
    /** 模型网格渲染组件 */
    export class MeshRenderer extends Renderer {
        /** 被渲染的模型网格 */
        public mesh: Nullable<Mesh>;

        public getVertexDataCount(): number {
            if (!this.mesh) return 0;
            return this.mesh.primitives.length;
        }

        public uploadVertexData(index: number, out: IRendererVDataResult) {
            if (!this.mesh || index >= this.mesh.primitives.length) return;
            //获取相应 vao
            const vao = this.mesh.VAO;
            if (!vao) return;
            const gl = App.webgl2;
            gl.bindVertexArray(vao);   //启用vao
            if (out) {
                const p = this.mesh.primitives[index];
                out.lenght = p.length;
                out.bufferOffset = p.bufferStart;
                out.elementType = this.mesh.elementType;
                out.drawMode = p.mode;
                out.frontFace = p.frontFace;
                out.material = p.material;
            }
        }

        public dispose(): void {
            super.dispose();
            this.mesh = null;
        }
    }
}